package com.autoplaza;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class MenuLateral extends Activity {
	public int[] images;
	public String clase = "";

	public void salir(View v) {
		finish();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_menu_lateral);
		clase = getIntent().getExtras().getString("clase");
		
		int size = Utils.getScreenSize2(MenuLateral.this);
		if( size == 1){
			RelativeLayout ll_parent = (RelativeLayout) findViewById(R.id.parent_linear_layout);
			ll_parent.setLayoutParams(new LinearLayout.LayoutParams(250, 300));
			
//			RelativeLayout lin = (RelativeLayout) findViewById(R.id.linearLayout1);
//			lin.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 30));
//			btnmenu.setLayoutParams(new LinearLayout.LayoutParams(30, 30));
		}		
		if( size == 2){
//			RelativeLayout lin = (RelativeLayout) findViewById(R.id.linearLayout1);
//			lin.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 60));	
//			btnmenu.setLayoutParams(new LinearLayout.LayoutParams(45, 45));
			RelativeLayout ll_parent = (RelativeLayout) findViewById(R.id.parent_linear_layout);
			ll_parent.setLayoutParams(new LinearLayout.LayoutParams(375, 600));

		}		
		if( size == 3){
//			RelativeLayout lin = (RelativeLayout) findViewById(R.id.linearLayout1);
//			lin.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 80));
//			btnmenu.setLayoutParams(new LinearLayout.LayoutParams(80, 80));
			RelativeLayout ll_parent = (RelativeLayout) findViewById(R.id.parent_linear_layout);
			ll_parent.setLayoutParams(new LinearLayout.LayoutParams(500, 900));

		}
		
		images = new int[8];
		images[0] = R.drawable.m_inicio;
		images[1] = R.drawable.m_contacto;
		images[2] = R.drawable.m_faq;
		images[3] = R.drawable.m_sugerencias;
		images[4] = R.drawable.m_facebook;
		images[5] = R.drawable.m_twitter;
		
		ListView lista = (ListView) findViewById(R.id.lista_fragment_menu);
		lista.setAdapter(new LazyAdapter(MenuLateral.this, new String[] {"Inicio","Contacto","Preguntas Frecuentes","Sugerencias","Facebook","Twitter",}));
		lista.setCacheColorHint(0);
		lista.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				String url = "";

				switch (position) {
				case 0:
					finish();
					if(!clase.equals("Buscar")){
						Intent a = new Intent(MenuLateral.this, Buscar.class);
				        a.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				        startActivity(a);	
					}
					break;
				case 1:
					finish();
					if(!clase.equals("ContactoFeedback")){
						Intent a = new Intent(MenuLateral.this, ContactoFeedback.class);
				        a.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				        startActivity(a);	
					}
					break;
				case 2:
					finish();
					if(!clase.equals("PreguntasFrecuentes")){
						Intent a = new Intent(MenuLateral.this, PreguntasFrecuentes.class);
				        a.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				        startActivity(a);	
					}

					break;
				case 3:
					finish();
					if(!clase.equals("ContactoFeedback")){
						Intent a = new Intent(MenuLateral.this, ContactoFeedback.class);
				        a.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				        startActivity(a);	
					}

					break;
				case 4:
					finish();
					if(!clase.equals("Facebook")){
						Intent a = new Intent(MenuLateral.this, Navegador.class);
						a.putExtra("url", "https://www.facebook.com/AutoplazaMexico?fref=ts");
						a.putExtra("red", "Facebook");
				        a.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				        startActivity(a);	
					}
					
//					Intent gourl = new Intent(MenuLateral.this, Navegador.class);
//					gourl.putExtra("url", "https://www.facebook.com/AutoplazaMexico?fref=ts");
//					startActivity(gourl);
//					finish();
					break;
				case 5:
					finish();
					if(!clase.equals("Twitter")){
						Intent a = new Intent(MenuLateral.this, Navegador2.class);
						a.putExtra("url", "https://twitter.com/AutoplazaMexico");
						a.putExtra("red", "Twitter");
				        a.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				        startActivity(a);	
					}
//					Intent gourl2 = new Intent(MenuLateral.this, Navegador.class);
//					gourl2.putExtra("url", "https://twitter.com/AutoplazaMexico");
//					startActivity(gourl2);
//					finish();
					break;

				default:
					break;
				}

			}
		});
	}

public class LazyAdapter extends BaseAdapter {
	    
	    private Activity activity;
	    private String[] data;
//	    private ImageView image;
	    private  LayoutInflater inflater=null;
//	    public ImageLoader imageLoader; 
	    
	    public LazyAdapter(Activity a, String[] d) {
	        activity = a;
	        data=d;
	        inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//	        imageLoader=new ImageLoader(activity.getApplicationContext());
	    }

	    public int getCount() {
	        return data.length;
	    }

	    public Object getItem(int position) {
	        return position;
	    }

	    public long getItemId(int position) {
	        return position;
	    }
	    
	    public View getView(int position, View convertView, ViewGroup parent) {
	        View vi=convertView;
	        if(convertView==null)
	            vi = inflater.inflate(R.layout.item, null);

	        int size = Utils.getScreenSize2(MenuLateral.this);
	        if(size == 1){
	        	TextView text=(TextView)vi.findViewById(R.id.text);;
	 	        ImageView image=(ImageView)vi.findViewById(R.id.image);
	 	        image.setLayoutParams(new LinearLayout.LayoutParams(30,30));
	 	        text.setText(""+data[position]);
	 	        image.setImageResource(images[position]);	
	        }
	        if(size == 2){
	        	TextView text=(TextView)vi.findViewById(R.id.text);;
	 	        ImageView image=(ImageView)vi.findViewById(R.id.image);
	 	        image.setLayoutParams(new LinearLayout.LayoutParams(80,80));
	 	        text.setText(""+data[position]);
	 	        image.setImageResource(images[position]);	
	        }
	        if(size == 3){
	        	TextView text=(TextView)vi.findViewById(R.id.text);;
	 	        ImageView image=(ImageView)vi.findViewById(R.id.image);
	 	        image.setLayoutParams(new LinearLayout.LayoutParams(100,100));
	 	        text.setText(""+data[position]);
	 	        image.setImageResource(images[position]);
	        }
	        
	       
//	        ImageView im = vi.findViewById(R.id.)
//	        imageLoader.DisplayImage(data[position], image);
	        return vi;
	    }
	}

	
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.menu_lateral, menu);
		return true;
	}

}
